package agent

import (
	"context"
	"sort"
	"sync"

	agent "gitee.com/liuxuezhan/ar-platform/rpc-client/agentClient/client"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/agentClient/rpc"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/gameManageClient/client/game"
	"github.com/spf13/cast"
	"github.com/zeromicro/go-zero/core/logc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type RebatesSettingListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 返佣配置列表
func NewRebatesSettingListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RebatesSettingListLogic {
	return &RebatesSettingListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RebatesSettingListLogic) RebatesSettingList(req *types.ListRequest) (resp *types.ListResponse, err error) {
	var (
		wg      sync.WaitGroup
		errs    = make([]error, 0)
		setting *agent.AgentRebatesSettingListResponse
		games   *game.GameCategoryListResp
	)
	wg.Add(2)
	go func() {
		var _err error
		defer func() {
			wg.Done()
			errs = append(errs, _err)
		}()
		setting, _err = l.svcCtx.AgentRPC.RebatesSetting(l.ctx, &rpc.AgentRebatesSettingListRequest{
			Type:     cast.ToInt32(req.Type),
			Page:     req.Page,
			PageSize: req.PageSize,
		})
	}()

	go func() {
		var _err error
		defer func() {
			wg.Done()
			errs = append(errs, _err)
		}()
		games, _err = l.svcCtx.GameManageRpc.GameCategoryList(l.ctx, &game.GameCategoryListReq{
			IsEnabled: 1,
		})
	}()
	wg.Wait()
	for _, _err := range errs {
		if _err != nil {
			logc.Error(l.ctx, _err)
			return nil, _err
		}
	}
	// 处理游戏类型
	_gameMap := make(map[string]struct {
		Id   int64
		Name string
		Code string
	})
	for _, _g := range games.List {
		_gameMap[_g.Code] = struct {
			Id   int64
			Name string
			Code string
		}{Id: _g.Id, Name: _g.Name, Code: _g.Code}
	}
	_data := make([]types.ListItem, len(setting.Data))
	for _i, _row := range setting.Data {
		_gameRate := make([]types.GameRate, 0)
		_tmpGame := make(map[string]types.GameRate)
		for _, _g := range _row.GamesRate {
			rate := types.GameRate{
				GameId: _g.GameId,
				Game:   _g.Game,
				Rate:   _g.Rate,
				Code:   _g.Code,
			}
			// 存在游戏类型的,才显示,过滤掉已经排出的游戏
			if _, ok := _gameMap[_g.Code]; ok {
				_tmpGame[_g.Code] = rate
			}
			// _gameRate = append(_gameRate, rate)
		}
		for _code, _g := range _gameMap {
			if _tmp, ok := _tmpGame[_code]; !ok {
				_tmpGame[_code] = types.GameRate{
					GameId: _g.Id,
					Game:   _g.Name,
					Code:   _g.Code,
				}
			} else {
				_tmp.Game = _g.Name
				_tmpGame[_code] = _tmp
			}
		}
		for _, _gm := range _tmpGame {
			_gameRate = append(_gameRate, _gm)
		}
		sort.Slice(_gameRate, func(i, j int) bool {
			return _gameRate[i].GameId < _gameRate[j].GameId
		})
		_data[_i] = types.ListItem{
			Id:           _row.Id,
			GamesRate:    _gameRate,
			LevelRate:    _row.LevelRate,
			LevelLimit:   _row.LevelLimit,
			ConditionNum: _row.ConditionNum,
			Remark:       _row.Remark,
			Type:         _row.Type,
		}
	}
	resp = &types.ListResponse{
		Total:    setting.Total,
		Page:     setting.Page,
		PageSize: setting.PageSize,
		Data:     _data,
	}
	return
}
